package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.dto.CoptdDtos;
import com.example.entity.dto.CoptnDto;
import com.example.entity.dto.CoptnDtos;
import com.example.entity.dto.PojoDto;
import com.example.entity.vo.CoptnVo;
import com.example.entity.vo.ZnxVo;
import com.example.entity.yifei.*;
import com.example.mapper.CoptdMapper;
import com.example.mapper.CoptjMapper;
import com.example.mapper.CoptoMapper;
import com.example.service.CoptnService;
import com.example.mapper.CoptnMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
* @author 14501
* @description 针对表【COPTN】的数据库操作Service实现
* @createDate 2024-08-08 16:06:08
*/
@Service
public class CoptnServiceImpl extends ServiceImpl<CoptnMapper, Coptn> implements CoptnService{

    @Resource
    private CoptnMapper coptnMapper;
    @Resource
    private CoptoMapper coptoMapper;
    @Resource
    private CoptdMapper coptdMapper;
    @Resource
    private CoptjMapper coptjMapper;

    /**
     * 查询出货通知单销货模块分页
     * @param current
     * @param size
     * @param code
     * @return
     */
    public Result queryIPage(Integer current, Integer size,String code) {
        try{
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(current,size);
            List<CoptnVo> coptnVoList = this.coptnMapper.queryList(code);
            PageInfo<CoptnVo> coptnVoPageInfo = new PageInfo<>(coptnVoList);
            map.put("pages", coptnVoPageInfo.getPages());
            map.put("total", coptnVoPageInfo.getTotal());
            map.put("list", coptnVoPageInfo.getList());
            return Result.success(map);
        }catch (Exception e){
            throw new YiFeiException(500,"查询出货通知单销货模块分页发生异常"+e.getMessage());
        }
    }

    /**
     * 查询出货通知单分页发生
     * @param current
     * @param size
     * @return
     */
    @Override
    public Result queryIPages(Integer current, Integer size, CoptnDto coptnDto) {
        try{
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(current,size);
            List<Coptn> coptnList = this.coptnMapper.queryIpage(coptnDto);
            PageInfo<Coptn> coptnPageInfo = new PageInfo<>(coptnList);
            map.put("pages", coptnPageInfo.getPages());
            map.put("total", coptnPageInfo.getTotal());
            map.put("list", coptnPageInfo.getList());
            return Result.success(map);
        }catch (Exception e){
            throw new YiFeiException(500,"查询出货通知单分页发生异常"+e.getMessage());
        }
    }

    /**
     * 查询出货通知单中客户订单分页
     * @param coptdDtos
     * @return
     */
    @Override
    public Result queryCopTc(CoptdDtos coptdDtos) {
        try{
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(coptdDtos.getCurrent(),coptdDtos.getSize());
            List<Coptd> coptdList = this.coptdMapper.queryCopTc(coptdDtos);
            PageInfo<Coptd> coptdPageInfo = new PageInfo<>(coptdList);
            map.put("pages", coptdPageInfo.getPages());
            map.put("total", coptdPageInfo.getTotal());
            map.put("list", coptdPageInfo.getList());
            return Result.success(map);
        }catch (Exception e){
            throw new YiFeiException(500,"查询出货通知单中客户订单分页发生异常"+e.getMessage());
        }
    }

    /**
     * 新增出货单通知单
     * @param coptn
     * @return
     */
    @Override
    @Transactional
    @Async
    public CompletableFuture<Result> saveCoptn(Coptn coptn) {
        synchronized (this) {
            try {
                int insert = this.coptnMapper.insert(coptn);
                if (insert > 0) {
                    if (!coptn.getCoptoList().isEmpty()) {
                        coptn.getCoptoList().forEach(arr -> {
                            this.coptoMapper.insert(arr);
                        });
                    }
                }
                return CompletableFuture.completedFuture(Result.success("出货通知单新增成功!"));
            } catch (Exception e) {
                throw new YiFeiException(500, "出货通知单新增发生异常" + e.getMessage());
            }
        }
    }

    /**
     * 修改出货通知单
     * @param coptn
     * @return
     */
    @Override
    @Transactional
    public Result updateCopTn(Coptn coptn){
        if (coptn==null){
            return Result.error("请输入正确参数");
        }
        int update = this.coptnMapper.update(coptn, new QueryWrapper<Coptn>().eq("TN001", coptn.getTN001()).eq("TN002", coptn.getTN002()));
        if (update>0) {
            if (coptn.getCoptoList()!=null){
                coptn.getCoptoList().forEach(arr -> {
                    this.coptoMapper.delete(new QueryWrapper<Copto>().eq("TO001", arr.getTO001()).eq("TO002", arr.getTO002()));
                    this.coptoMapper.insert(arr);
                });
            }
        }
        return Result.success();
    }

    @Override
    @Transactional
    public Result deleteCopTn(String tn001, String tn002) {
        try {
            List<Coptj> coptjList = this.coptjMapper.selectList(new LambdaQueryWrapper<Coptj>().eq(Coptj::getTj044, tn001).eq(Coptj::getTj045, tn002));
            if (!coptjList.isEmpty()){
                return Result.error("该销货通知到有销货单后置单据,无法进行删除");
            }
            int delete = this.coptnMapper.delete(new LambdaQueryWrapper<Coptn>().eq(Coptn::getTN001, tn001).eq(Coptn::getTN002, tn002));
            if (delete>0){
                int delete1 = this.coptoMapper.delete(new LambdaQueryWrapper<Copto>().eq(Copto::getTO001, tn001).eq(Copto::getTO002, tn002));
                if (delete1>0){
                    return Result.success(null,"出货通知单删除成功!");
                }
            }
            return null;
        }catch (Exception e){
            throw new YiFeiException(500, "出货通知单删除发生异常"+e.getMessage());
        }
    }

    @Override
    public Result queryCopToTnList(Integer current,Integer size,CoptnDto coptnDto) {
        try {
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(current,size);
            List<Map<String, Object>> maps = this.coptnMapper.queryCopToTnList(coptnDto);
            PageInfo<Map<String,Object>> coptnPageInfo = new PageInfo<>(maps);
            map.put("pages", coptnPageInfo.getPages());
            map.put("total", coptnPageInfo.getTotal());
            map.put("list", coptnPageInfo.getList());
            return Result.success(map);
        }catch (Exception e){
            throw new YiFeiException(500, "出货通知单单头单身分页查询发生异常"+e.getMessage());
        }
    }

    @Override
    public Result queryCopTnToLy(CoptnDtos coptnDtos) {
        try {
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(coptnDtos.getCurrent(),coptnDtos.getSize());
            List<Coptn> coptnList = this.coptnMapper.queryCopTnToLy(coptnDtos.getCode());
            PageInfo<Coptn> coptnPageInfo = new PageInfo<>(coptnList);
            if (!coptnPageInfo.getList().isEmpty()){
                coptnPageInfo.getList().forEach(coptn->{
                    List<Copto> coptoList = coptoMapper.queryList(coptn.getTN001(),coptn.getTN002());
                    coptn.setCoptoList(coptoList);
                });
            }
            map.put("pages", coptnPageInfo.getPages());
            map.put("total", coptnPageInfo.getTotal());
            map.put("list", coptnPageInfo.getList());
            return Result.success(map);
        }catch (Exception e){
            throw new YiFeiException(500, "出货通知单(列印)查询,出货通知单的单头和单身分页查询发生异常"+e.getMessage());
        }
    }

}




